package com.xn.app.muying.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.xn.app.muying.R;
import com.xn.app.muying.adpter.MineYiYuanAdapter;
import com.xn.app.muying.bean.MineYiYuanBean;
import com.xn.app.muying.fragment.MineFragment;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/19.
 */
public class JiuZhenActivity extends AppCompatActivity implements HttpUtil.OnGetDataListener, AdapterView.OnItemClickListener {
    private Handler handler = new Handler();
    private ListView lv;
    private MineYiYuanAdapter adapter;
    ArrayList<MineYiYuanBean> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jiuzhen);
        lv = (ListView) findViewById(R.id.jiuzhen_activity_lv);
        HttpUtil.getData(APIMethod.Mine_JiuZhenYiYuan, handler, this);
        adapter = new MineYiYuanAdapter(this);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);

    }

    @Override
    public void onJsonData(String data) {
        try {
            JSONObject jsonObject = new JSONObject(data);
            JSONArray items = jsonObject.getJSONArray("items");
            list = new ArrayList<>();
            for (int i = 0; i < items.length(); i++) {
                JSONObject object = items.getJSONObject(i);
                String name = object.optString("name");
                int id = object.optInt("id");
                MineYiYuanBean bean = new MineYiYuanBean();
                bean.setName(name);
                bean.setId(id);
                list.add(bean);

            }
            adapter.getData(list);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent1 = getIntent();
        String tag = intent1.getStringExtra("tag");
        Intent intent = new Intent();
//如果从孕期信息页面(YunQiXuanZeActivity)进入，执行if，如果从我的页面(Mine)进入，执行else
        if (tag != null && tag.equals(1 + "")) {
            intent.putExtra("yiyuan", list.get(i).getName());
            intent.putExtra("id", list.get(i).getId());
            intent.putExtra("home_page","1");
            intent.setClass(JiuZhenActivity.this, YunChanSheZhiActivity.class);
            startActivity(intent);
            Intent intent2 =new Intent();
            intent2.setAction("hosptial");
            intent2.putExtra("name",list.get(i).getName());
            sendBroadcast(intent2);
            finish();

        } else {
            intent.putExtra("yiyuan", list.get(i).getName());
            intent.putExtra("id", list.get(i).getId());
            setResult(2, intent);
            finish();
        }

    }
}
